package cn.com.sbell.vss.csg.vap;

import org.dom4j.Element;

import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import cn.com.sbell.vss.VssConstants;
import cn.com.sbell.vss.VssException;
import cn.com.sbell.vss.csg.vsclient.VsClient;
import cn.com.sbell.vss.csg.vsclient.util.PropertiesHolder;
import cn.com.sbell.vss.csg.vap.bean.AHInfo;
public class SetAlarmHostDefence extends ProtectedVsCommander implements AHCommand{

	@Override
	protected Element executeCommand(VsClient vsClient, PropertiesHolder holder, Element root) {
		// TODO Auto-generated method stub
		AHInfo ahInfo=getAhInfo(holder);
		AHInfo ahInfoReturn=vsClient.SetAlarmHostInfo(ahInfo);
		root.addElement("deviceId").setText(ahInfoReturn.getDeviceId());
		root.addElement("serverIp").setText(ahInfoReturn.getServerIp());
		root.addElement("serverPort").setText(ahInfoReturn.getServerPort());
		root.addElement("endZone").setText(Integer.toString(ahInfoReturn.getEndZone()));
		
		AHInfo.ZoneStatus[] zones=ahInfoReturn.getZoneStatus();
		if(null!=zones)
		{
			for (int i = 0; i < zones.length; i++) {
				Element zoneStatus=root.addElement("zoneStatus");
				zoneStatus.addElement("zoneNumber").setText(Integer.toString(zones[i].getZoneNumber()));
				zoneStatus.addElement("defenceStatus").setText(Integer.toString(zones[i].getDefenceStatus()));
				zoneStatus.addElement("subResult").setText(Integer.toString(zones[i].getSubResult()));
			}
		}
		return root;
	}
private static AHInfo getAhInfo(PropertiesHolder holder)
{
	AHInfo ahInfo=new AHInfo();
	ahInfo.setDeviceId(holder.getStrProperty("deviceId"));
	ahInfo.setServerIp(holder.getStrProperty("serverIp"));
	ahInfo.setServerPort(holder.getStrProperty("serverPort"));
	ahInfo.setDefenceStatus(Integer.parseInt(holder.getStrProperty("defenceStatus")));
	Map zones=new HashMap();
	String[] zoneStatus=holder.getStrProperties("zoneStatus");
	if(null!=zoneStatus){
	for (int i = 0; i < zoneStatus.length; i++) {
		String[] temp=zoneStatus[i].split(",");
		if(temp.length<2)
		{
			continue;
		}
		AHInfo.ZoneStatus zone=new AHInfo.ZoneStatus();
		zones.put(temp[0],zone);
		zone.setZoneNumber(Integer.parseInt(temp[0]));
		zone.setDefenceStatus(Integer.parseInt(temp[1]));
	}
	}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//channel and status should match
//
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//	String[] zoneDefence=holder.getStrProperties("defenceStatus");
//	for (int i = 0; i < zoneDefence.length; i++) {
//		String[] temp=zoneDefence[i].split(",");
//		AHInfo.ZoneStatus zone=null;
//		if((temp.length<2)||(null==(zone=(AHInfo.ZoneStatus)zones.get(temp[0]))))
//		{
//			continue;
//		}
//		zone.setDefenceStatus(Integer.parseInt(temp[1]));
//	}
	List tempZones=new ArrayList();
	for (Iterator iter = zones.entrySet().iterator(); iter.hasNext();) {
		Map.Entry element = (Map.Entry) iter.next();
		AHInfo.ZoneStatus az=(AHInfo.ZoneStatus)element.getValue();
		tempZones.add(az);
	}
	ahInfo.setZoneStatus((AHInfo.ZoneStatus[])tempZones.toArray(new AHInfo.ZoneStatus[0]));
	return ahInfo;
}

}
